﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using FILSTARTUPS_CMS.Models;
using FILSTARTUPS_CMS.Helpers;

namespace FILSTARTUPS_CMS.Controllers
{
    [AdminAccess]
    public class RoleController : Controller
    {
        FilStartUpsEntities cms_dbconnect = new FilStartUpsEntities();
        //
        // GET: /Role/

        public ActionResult Index()
        {
            var role = cms_dbconnect.Roles.OrderBy(r => r.roleidPK).ToList();
            return View(role);
        }

        public ActionResult Create()
        {
            return View();

        }
        [HttpPost]
        public ActionResult Create(Role role)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    role.datecreated = DateTime.Today;
                    cms_dbconnect.Roles.Add(role);
                    TempData["message"] = string.Format("{0} has been saved", role.Rolename);
                    cms_dbconnect.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {

            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            Role role = cms_dbconnect.Roles.Find(id);
            return View(role);
        }
        [HttpPost]
        public ActionResult Edit(Role role)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    role.datecreated = DateTime.Today;
                    cms_dbconnect.Entry(role).State = EntityState.Modified;
                    cms_dbconnect.SaveChanges();
                    TempData["message"] = string.Format("{0} has been modified", role.Rolename);
                    return RedirectToAction("Index");
                }
                else
                {
                    return Content("Please review your form");
                }
            }
            catch
            {

            }
            return View();
        }

        public ActionResult Delete(int id)
        {
            Role role = cms_dbconnect.Roles.Find(id);
            try
            {
                cms_dbconnect.Roles.Remove(role);
                cms_dbconnect.SaveChanges(); 
                TempData["message"] = string.Format("{0} has been deleted", role.Rolename); 
                return RedirectToAction("Index");
            }
            catch
            {

            }
            return View();
        }
    }
}
